<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
		body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
	</style>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=EX8DyYmj9Mmm14kjdz1wYk7vYO0CCTDy"></script>
	<title>IP定位获取当前城市</title>
</head>
<body>
	<div id="allmap">
	</div>
	<p sty>添加点击地图监听事件，点击地图后显示当前经纬度</p>
</body>
</html>
<script type="text/javascript">
	//百度地图获取坐标
	function getAddr() {
		var map = new BMap.Map("allmap");
		var geolocation = new BMap.Geolocation();
		var pt;
		geolocation.getCurrentPosition(function(r) {
			if (this.getStatus() == BMAP_STATUS_SUCCESS) {
				//setCookie("localX", r.point.lng);
				//setCookie("localY", r.point.lat);
				alert(r.point.lng + " ， " + r.point.lat);
				pt = r;
				showPosition(pt);
				//传参数给地图按钮
				//document.getElementById("alinkMap").href += "&x="+r.point.lng+"&y="+r.point.lat;
				//document.getElementById("alinkMap").style.display="block";
			}
		});
	}
	//百度地图WebAPI 坐标转地址
	function showPosition(r) {
		// ak = appkey 访问次数流量有限制
		var url = 'http://api.map.baidu.com/geocoder/v2/?ak=7b788c5ea45cc4b3ac6331a4b0643d5b&callback=?&location='
				+ r.point.lat + ',' + r.point.lng + '&output=json&pois=1';
		$.getJSON(url, function(res) {
			$("#msg").html(url);
			alert(res.result.addressComponent.city);
		});
	}
	//百度地图JS API 坐标转地址，没有加载地图时获取不到rs,总是null
	function getLocation(myGeo, pt, rs) {
		// 根据坐标得到地址描述 
		myGeo.getLocation(pt, function(rs) {
			if (rs) {
				var addComp = rs.addressComponents;
				window.clearInterval(interval);
				alert(addComp);
			}
			return rs;
		});
	}

	function showInfo(e) {
		alert(e.point.lng + ", " + e.point.lat);
	}
	map.addEventListener("click", showInfo);
</script>
